package com.strato.rule.core.v1_0_0.execution;

import java.util.LinkedHashMap;
import java.util.Map;

public class Session {

	private RuleEngine ruleEngine;

	public Session(RuleEngine ruleEngine){
		this.ruleEngine=ruleEngine;
	}
	
	private Map<String,Object> facts=new LinkedHashMap<String,Object>();
	
	public void addFact(String name,Object fact){
		facts.put(name, fact);
	}
	
	public void deleteFact(String name){
		facts.remove(name);
	}
	
	public Object getFact(String name){
		return facts.get(name);
	}
	
	public Object getFact(Class<?> clazz){
		for(Object fact:facts.values()){
			if(clazz.isInstance(fact)){
				return fact;
			}
		}
		return null;
	}
 	
	private Map<String,Object> globals=new LinkedHashMap<String,Object>();
	
	public void addGlobal(String name,Object global){
		globals.put(name, global);
	}
	
	public void deleteGlobal(String name){
		globals.remove(name);
	}
	
	public Object getGlobal(String name){
		return globals.get(name);
	}
	
	public RuleEngine getRuleEngine() {
		return ruleEngine;
	}

	public void execute(RuleSet ruleSet) throws RuleExecutionException{
		ruleSet.execte(this);
	}
}
